package com.yyds.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyds.common.MyJSON;
import com.yyds.mapper.UserUserMapper;
import com.yyds.pojo.UserUser;
import com.yyds.service.UserUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//普通用户服务实现层
@Service("userUserService")
public class UserUserServiceImpl implements UserUserService {
    @Autowired
    private UserUserMapper userUserMapper;
    @Override
    public int addUserUser(UserUser userUser) {

        return userUserMapper.addUserUser(userUser);
    }

    @Override
    public int deleteUserUser(Integer user_id) {

        return userUserMapper.deleteUserUser(user_id);
    }

    @Override
    public int updateuseruser(UserUser userUser) {

        return userUserMapper.updateuseruser(userUser);
    }

    @Override
    public MyJSON<UserUser> queryalluseruser(Map<String, Object> queryuser) {
//        配置分页
        Integer page = Integer.parseInt(queryuser.get("page")+"");
        Integer limit = Integer.parseInt(queryuser.get("limit")+"");
        PageHelper.startPage(page,limit);
        List<UserUser> signUserList =  userUserMapper.queryalluseruser(queryuser);
        // mybatis 的分页数据结构
        PageInfo<UserUser> signUserPageInfo = new PageInfo<>(signUserList);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        MyJSON<UserUser> signUserMyJSON = new MyJSON<>();
        signUserMyJSON.setCode(0);
        signUserMyJSON.setMsg("学生分页列表数据");
        signUserMyJSON.setCount(signUserPageInfo.getTotal());  // 总记录数
        signUserMyJSON.setData(signUserPageInfo.getList());  // 分页列表数据

        return signUserMyJSON;
    }

    @Override
    public Integer deletesuseruser(String str) {
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
        }
        return userUserMapper.deletesuseruser(list);
    }

    @Override
    public UserUser findPasswordByUser(String user_name) {
        return userUserMapper.findPasswordByUser(user_name);
    }
}
